package com.zhongyouyun.admin.controller;


import cn.hutool.core.util.ObjectUtil;
import com.zhongyouyun.admin.model.MenuModel;
import com.zhongyouyun.admin.vo.MenuVo;
import com.zhongyouyun.other.annotation.XianMuAuthScope;
import com.zhongyouyun.other.model.Result;
import com.zhongyouyun.other.model.RoleMenuOrDict;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zhongyouyun.admin.service.RoleMenuService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @Author: shenyi
 * @Date: 2021-09-23
 * @Description: 角色资源中间表
 */
@RestController
@RequestMapping("/admin/z_role_menu")
@Api(value = "角色菜单controller", tags = {"角色菜单菜单接口"})
public class RoleMenuController {
    @Autowired
    private RoleMenuService service;

    @GetMapping("/findMenuALLTreeList")
    @ResponseBody
    @ApiOperation(value = "获取角色所有菜单列表", notes = "获取角色所以菜单列表")
    @XianMuAuthScope(deptAlias = "temp_data_scope.")
    public Result<MenuVo> findMenuALLTreeList(@ApiParam(name = "角色ID", value = "id") @RequestParam(value = "id") String id) {
        MenuVo menuVo = service.findMenuALLTreeList(id);
        if (ObjectUtil.isNotEmpty(menuVo)) {
            return Result.<MenuVo>builder().message("成功").status(0).data(menuVo).build();
        } else {
            return Result.<MenuVo>builder().message("没有任何数据").success(false).status(1).build();
        }
    }

    @GetMapping("/findMenuTreeList")
    @ResponseBody
    @ApiOperation(value = "获取角色已选菜单列表", notes = "获取角色已选菜单列表")
    @XianMuAuthScope(deptAlias = "temp_data_scope.")
    public Result<List<MenuModel>> findRoleMenuTree(@ApiParam(name = "角色ID", value = "id") @RequestParam(value = "id") String id) {
        List<MenuModel> list = service.selectRoleMenuList(id);
        if (null != list && list.size() > 0) {
            return Result.<List<MenuModel>>builder().message("成功").status(0).data(list).build();
        } else {
            return Result.<List<MenuModel>>builder().status(0).build();
        }
    }

    @PostMapping("/saveOrUpate")
    @ResponseBody
    @ApiOperation(value = "修改或者新增", notes = "通过角色ID修改角色权限")
    public Result saveOrUpate(@RequestBody List<RoleMenuOrDict> roleMenuOrDictList, HttpServletRequest request) {
        String type = request.getHeader("xiangmuType");
        Boolean flag = service.saveOrUpdateforRoleMenuModel(roleMenuOrDictList,type);
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("保存失败").success(false).status(1).build();
        }
    }


}
